<?

class graphMemoryUsage {
	private $device;
	private $rrdPath =  '';
	private $bgColor = '#fff';
	public $start = '-1d';
	public $height = 0;
	public $width = 0;
	public $legend = true;
	public function __construct(&$device,$params=array()) {
		$this->device = $device;
		$this->rrdPath = $device->getRrdPath().'memory_usage.rrd';
		if (is_array($params) || is_object($params)) {
			
			foreach ($params AS $name=>$value) {
				switch ($name) {
					case 'start':
						$this->$name = $value;
						break;
					case 'width':
					case 'height':
						$this->$name = (int)$value;
						break;
					case 'legend':
						$this->$name = (bool)$value;
						break;
						
				}
			}
		}
		
	}
	
	public function generate($path) {
		$opts =array();
		$opts[] = '--start';
		$opts[] = $this->start;	
		$opts[] = '--full-size-mode'; 
		$opts[] = '--slope-mode';
		$opts[] = '-b';
		$opts[] = '1024';
		$opts[] = '-c';
		$opts[] = 'BACK#fff';
		$opts[] = '-c';
		$opts[] = 'SHADEA#fff';
		$opts[] = '-c';
		$opts[] = 'SHADEB#fff';
		$opts[] = '-c';
		$opts[] = 'MGRID#999';
		$opts[] = '-c';
		$opts[] = 'GRID#ccc';
		$opts[] = '-c';
		$opts[] = 'ARROW#fff';
		$opts[] = '-c';
		$opts[] = 'FONT#999';
		$opts[] = '--rigid';
		$opts[] = '--font';
		$opts[] = 'AXIS:6:mono';
		$opts[] = '--font';
		$opts[] = 'UNIT:6:mono';
		$opts[] = '--font';
		$opts[] = 'LEGEND:7:mono';
		if (!empty($this->height) && $this->height < 50) { $opts[] = '--only-graph';}
		if (!empty($this->height)) { $opts[] = '--height';$opts[] = $this->height; }
		if (!empty($this->width)) { $opts[] = '--width';$opts[] = $this->width; }		
		if (!$this->legend) { $opts[] = '--no-legend';}
		$opts[] = 'DEF:atotalswap='.$this->rrdPath.':totalswap:AVERAGE';
		$opts[] = 'DEF:aavailswap='.$this->rrdPath.':availswap:AVERAGE';
		$opts[] = 'DEF:atotalreal='.$this->rrdPath.':totalreal:AVERAGE';
		$opts[] = 'DEF:aavailreal='.$this->rrdPath.':availreal:AVERAGE';
		$opts[] = 'DEF:atotalfree='.$this->rrdPath.':totalfree:AVERAGE';
		$opts[] = 'DEF:ashared='.$this->rrdPath.':shared:AVERAGE';
		$opts[] = 'DEF:abuffered='.$this->rrdPath.':buffered:AVERAGE';
		$opts[] = 'DEF:acached='.$this->rrdPath.':cached:AVERAGE';
		$opts[] = 'CDEF:totalswap=atotalswap,1024,*';
		$opts[] = 'CDEF:availswap=aavailswap,1024,*';
		$opts[] = 'CDEF:totalreal=atotalreal,1024,*';
		$opts[] = 'CDEF:availreal=aavailreal,1024,*';
		$opts[] = 'CDEF:totalfree=atotalfree,1024,*';
		$opts[] = 'CDEF:shared=ashared,1024,*';
		$opts[] = 'CDEF:buffered=abuffered,1024,*';
		$opts[] = 'CDEF:cached=acached,1024,*';
		$opts[] = 'CDEF:usedreal=totalreal,availreal,-';
		$opts[] = 'CDEF:usedswap=totalswap,availswap,-';
		$opts[] = 'CDEF:cusedswap=usedswap,1,*';
		$opts[] = 'CDEF:cdeftot=availreal,shared,buffered,usedreal,cached,usedswap,+,+,+,+,+';		

		//$opts[] = 'LINE1:totalreal#050505:total';
		//$opts[] = 'GPRINT:totalreal:AVERAGE:  %7.2lf%sB';

		$opts[] = 'LINE1:totalreal#050505:total';
		


		$opts[] = 'AREA:cusedswap#C3D9FF:swap';		
		$opts[] = 'LINE1.25:cusedswap#356AA0::';
		$opts[] = 'LINE1:totalreal#050505:';
		//$opts[] = 'GPRINT:usedswap:LAST:   %7.2lf%sB';
		//$opts[] = 'GPRINT:usedswap:AVERAGE:%7.2lf%sB';
		//$opts[] = 'GPRINT:usedswap:MAX:%7.2lf%sB\n';
		


		$opts[] = 'AREA:usedreal#f0e0a0:used\n';
		$opts[] = 'STACK:availreal#e5e5e5:free\n';
		//$opts[] = 'LINE1.25:cached#ea8f00:cached:STACK';


		$opts[] = 'AREA:shared#afeced::';
		$opts[] = 'LINE1.25:shared#008fea:shared\n';

		$opts[] = 'AREA:buffered#cc0000::STACK';
		$opts[] = 'LINE1.25:buffered#ff1a00:buffers:STACK';
		$opts[] = 'AREA:cached#ffaa66::STACK';
		$opts[] = 'LINE1:usedreal#d0b080:';



//		$opts[] = 'GPRINT:cached:LAST: %7.2lf%sB';
//		$opts[] = 'GPRINT:cached:AVERAGE:%7.2lf%sB';
//		$opts[] = 'GPRINT:cached:MAX:%7.2lf%sB\n';
		
		//';
//		$opts[] = 'GPRINT:buffered:LAST:%7.2lf%sB';
//		$opts[] = 'GPRINT:buffered:AVERAGE:%7.2lf%sB';
//		$opts[] = 'GPRINT:buffered:MAX:%7.2lf%sB\n';
		

//		
		
//		

		//
		//$opts[] = 'GPRINT:shared:LAST: %7.2lf%sB';
		//$opts[] = 'GPRINT:shared:AVERAGE:%7.2lf%sB';
		//$opts[] = 'GPRINT:shared:MAX:%7.2lf%sB\n';


		
		
		//
		$opts[] = 'LINE1:usedreal#d0b080:';
//		$opts[] = 'GPRINT:usedreal:LAST:   %7.2lf%sB';
//		$opts[] = 'GPRINT:usedreal:AVERAGE:%7.2lf%sB';
//		$opts[] = 'GPRINT:usedreal:MAX:%7.2lf%sB\n';
		
		//		$opts[] = 'GPRINT:availreal:LAST:   %7.2lf%sB';
//		$opts[] = 'GPRINT:availreal:AVERAGE:%7.2lf%sB';
//		$opts[] = 'GPRINT:availreal:MAX:%7.2lf%sB\n';

		
		

		
		

		
		$opts[] = 'COMMENT:Usage       Current     Average    Maximum\n';

		if (rrd_graph($path,$opts,count($opts)) == 0) {
			//echo rrd_error();		
		} else {

		}
	
	
	}

}